/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import control.MStockMidlet;
import font.MyFont;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import listener.Command;
import listener.ICommandListener;
import listener.IKeyListener;
import util.ImageUtils;
import util.SoftKey;

/**
 *
 * @author kien.tran
 */
public class Menu extends Canvas {

    private Display display;
    private Command ok, exit;
    private ICommandListener commandListener;
    private IKeyListener keyListener;
    private Image images[] = null;
    private Image background = null;
    private MyFont font;
    private int imageY = 0;//toa do y cac image se ve
    private int imageX = 0;//toa do x cac image se ve
    private int spaceWidth = 0;//khoang cach dai giua 2 image
    private int spaceHeight = 0;//khoang cach cao giua 2 image
    private int screenWidth;//w man hinh
    private int screenHeight;//h man hinh
    private int imageWidth;//w image
    private int imageHeight;//h imag
    private int titleHeight, commandHeight;// chieu cao tieu de
    private int numColum;
    private int numRow;
    private int currentIndex = 0;
    private Style bodyStyle, headerStyle, footerStyle;
    private String[] lang;
    private String text;
    private int imageLength;

    public Menu(Display display, MStockMidlet control, int numColum, String[] lang) {
        setFullScreenMode(true);
        this.display = display;
        this.lang = lang;
        this.text = lang[0];
        this.images = control.getImages();
        this.background = control.getBackground();
        this.screenWidth = getWidth();
        this.screenHeight = getHeight();
        this.font = control.getFont();
        bodyStyle = control.getStyle().getBodyStyle();
        headerStyle = control.getStyle().getHeaderStyle();
        footerStyle = control.getStyle().getFooterStyle();
        this.titleHeight = font.getFontNormal().getHeight() + headerStyle.getPadding().getTopAndBottom();
        this.commandHeight = font.getFontNormal().getHeight() + footerStyle.getPadding().getTopAndBottom();

        if (images != null) {
            imageLength = images.length;
        }

        Image temp = ImageUtils.getImage(images);
        if (temp != null) {
            this.imageWidth = temp.getWidth();
            this.imageHeight = temp.getHeight() + font.getFontNormal().getHeight() + 1;
        }

        this.numColum = numColum;
        this.numRow = imageLength / numColum;// số dòng =số icon/số cột
        if (imageLength % numColum != 0) {
            this.numRow++;
        }
        this.spaceWidth = (screenWidth - imageWidth * numColum) / (numColum + 1);  //khoang cach dài giua 2 image
        this.spaceHeight = (screenHeight - titleHeight - commandHeight - imageHeight * numRow) / (numRow + 1);//khoang cach cao giua 2 image
    }

    public int getCurrentIndex() {
        return this.currentIndex;
    }

    public void selectItem(int step) {
        currentIndex += step;
        if (currentIndex < 0) {
            currentIndex += imageLength;
        } else {
            currentIndex = currentIndex % imageLength;
        }
    }

    public void setText() {
        this.text = lang[currentIndex];
    }

    public void paint(Graphics g) {
        // draw bankground
        g.setColor(bodyStyle.getBgColor());
        g.fillRect(0, 0, screenWidth, screenHeight);
//        if (background != null) {
//            g.drawRegion(background, 0, 0, this.screenWidth, this.screenHeight, Sprite.TRANS_NONE, 0, 0, Graphics.TOP | Graphics.LEFT);
//        }

        int count = 1;
        imageY = titleHeight;
        imageX = 0;

        for (int i = 0; i < imageLength; i++) {
            if (count > numColum) {
                imageY += imageHeight + spaceHeight;
                imageX = 0;
                count = 1;
            }
            g.setColor(i == currentIndex ? bodyStyle.getSelectedBgColor() : bodyStyle.getBgColor());
            g.fillRoundRect(imageX + spaceWidth - 4, imageY + spaceHeight - 4, imageWidth + 8, imageHeight + 8, 10, 10);
            g.setColor(i == currentIndex ? headerStyle.getBgColor() : bodyStyle.getBgColor());
            g.drawRoundRect(imageX + spaceWidth - 4, imageY + spaceHeight - 4, imageWidth + 7, imageHeight + 7, 10, 10);
            if (images[i] != null) {
                g.drawImage(images[i], imageX + spaceWidth, imageY + spaceHeight - 1, Graphics.TOP | Graphics.LEFT);

                // TEST
                g.setColor(0x000000);
                font.getFontNormal().drawString(g, lang[i], imageX + spaceWidth + images[i].getWidth() / 2, imageY + spaceHeight + images[i].getHeight() + 1, Graphics.TOP | Graphics.HCENTER);
            }
            imageX += imageWidth + spaceWidth;
            count++;
        }

        paintTitle(g);
        paintFooter(g);
    }

    public void keyPressed(int key) {
        if (key == SoftKey.getLeftSoftkeyCode(this)) {
//      if (key == -6) {
            commandListener.commandAction(ok);

        } else if (key == SoftKey.getRightSoftkeyCode(this)) {
//        } else if (key == -7) {
            commandListener.commandAction(exit);
        } else {
            switch (key) {
                case KEY_NUM0:
                case KEY_NUM1:
                case KEY_NUM2:
                case KEY_NUM3:
                case KEY_NUM4:
                case KEY_NUM5:
                case KEY_NUM6:
                case KEY_NUM7:
                case KEY_NUM8:
                case KEY_NUM9:
                case KEY_STAR:
                case KEY_POUND:
                case FIRE:
                    keyListener.keyAction(key);
                    break;
                default:
                    int gameAction = getGameAction(key);
                    switch (gameAction) {
                        case RIGHT:
                        case LEFT:
                        case UP:
                        case DOWN:
                            if (gameAction == Canvas.RIGHT) {
                                selectItem(1);
                            } else if (gameAction == Canvas.LEFT) {
                                selectItem(-1);
                            } else if (gameAction == Canvas.UP) {
                                int r = currentIndex / numColum, i = 0;
                                r--;
                                if (r < 0) {
                                    r += numRow;
                                }
                                i = r * numColum + currentIndex % numColum;
                                if (i >= imageLength) {
                                    i = (r - 1) * numColum + currentIndex % numColum;
                                }
                                currentIndex = i;

                            } else {
                                int i = currentIndex + numColum;
                                if (i >= imageLength) {
                                    currentIndex = i % numColum;
                                } else {
                                    selectItem(numColum);
                                }
                            }
                            setText();
                            repaint();
                            break;

                        case FIRE:
                            keyListener.keyAction(FIRE);
                            break;
                    }
                    break;
            }
        }
    }

    public void setCommandListener(ICommandListener commandListener) {
        this.commandListener = commandListener;
    }

    public void setKeyListener(IKeyListener keyListener) {
        this.keyListener = keyListener;
    }

    public void setOkCommand(Command cmd) {
        this.ok = cmd;
    }

    public void setExitCommand(Command cmd) {
        this.exit = cmd;
    }

    public void show() {
        this.display.setCurrent(this);
    }

    public void paintTitle(Graphics g) {
        // paint bg
        g.setColor(headerStyle.getBgColor());
        g.fillRect(0, 0, screenWidth, titleHeight);

        // TEST
        g.setColor(0x000000);
        // paint text
        font.getFontNormal().drawString(g, lang[lang.length - 1], screenWidth / 2, headerStyle.getPadding().getTop(), Graphics.TOP | Graphics.HCENTER);
    }

    public void paintFooter(Graphics g) {
        if (ok != null || exit != null) {
            // paint bg
            g.setColor(footerStyle.getBgColor());
            g.fillRect(0, screenHeight - commandHeight, getWidth(), commandHeight);

            // paint ok command
            if (ok != null) {
                // TEST
                g.setColor(0x000000);
                font.getFontNormal().drawString(g, ok.getText(), footerStyle.getPadding().getLeft(), screenHeight - commandHeight + footerStyle.getPadding().getTop(), Graphics.TOP | Graphics.LEFT);
            }

            // paint right command
            if (exit != null) {
                // TEST
                g.setColor(0x000000);
                font.getFontNormal().drawString(g, exit.getText(), screenWidth - footerStyle.getPadding().getRight(), screenHeight - commandHeight + footerStyle.getPadding().getTop(), Graphics.TOP | Graphics.LEFT);
            }
        }
    }
}
